package com.wei.service_msm.controller;

import cn.hutool.core.util.StrUtil;
import com.wei.service_msm.service.MsmService;
import com.wei.service_msm.util.ConstantPropertiesUtil;
import com.wei.service_msm.util.RandomUtil;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author wjx
 * @ClassName MsmController
 * @description: TODO
 * @date 2022-12-03 08:44
 * @version: 1.0
 */
@RestController
@RequestMapping("/edumsm/message")
public class MsmController {

    @Autowired
    private MsmService messageService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/send/{phone}")
    public R sendCode(@PathVariable String phone) {
        //从redis中获取当前手机号的验证码
        String phoneCode = redisTemplate.opsForValue().get(phone);

        if (StrUtil.isNotBlank(phoneCode)) {
            return R.ok();
        }

        //没有验证码，则生成一个，并通过腾讯云短信服务发送给用户
        String randomNum = RandomUtil.getFourBitRandom();

//        boolean flag = messageService.send(phone,randomNum);
        System.out.println("======验证码是：" + randomNum);
        //将验证码存在redis中
//        if (flag){
        //发送成功，则将验证码保存到redis中
        redisTemplate.opsForValue().set(phone, randomNum, 5, TimeUnit.MINUTES);

        return R.ok();
//        }else {
//            //发送失败
//            return R.error();
//        }
    }
}
